DP N
Attach adjacent objects
Cost varies depending on the order of attachment.
Cost minimization issues
DP with the domain as the domain of definition
Same as [DP L
DP as the value of the lowest cost to attach the area when the area is specified.
code:python
def solve(N, XS):
accum = list(accumulate(XS)) + 0 @lru_cache(maxsize=None)
def sub(L, R):
if L == R:
return 0
ret = INF
for x in range(L, R):
v = sub(L, x) + sub(x + 1, R)
if v < ret:
ret = v
return ret + accumR - accumL - 1 return sub(0, N - 1)
Cython
code:python
cdef sub(long L, long R):
cdef long long ret
if L == R:
return 0
if ret != 0:
return ret
ret = INF
for x in range(L, R):
v = sub(L, x) + sub(x + 1, R)
if v < ret:
ret = v
return ret
cdef solve(N, XS):
cdef long i
cdef long long v
v = 0
for i in range(N):
return sub(0, N - 1)
---
This page is auto-translated from /nishio/DP N. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. I'm very happy to spread my thought to non-Japanese readers.